/**
 * Created by Administrator on 2016/12/20 0020.
 */
'use strict';
var React = require('react-native');
var bleManager = React.NativeModules.BleManager;
var CommandServiceUUID = '2010'
var CommandCharacteristicUUID = '1092'

var PowerServiceUUID = '180F'
var PowerCharacteristicUUID = '2A19'

class BleManager {

    constructor() {
        this.isPeripheralConnected = this.isPeripheralConnected.bind(this);
    }

    read(peripheralId, serviceUUID, characteristicUUID) {
        return new Promise((fulfill, reject) => {
            bleManager.read(peripheralId, serviceUUID, characteristicUUID, (error, data) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(data);
                }
            });
        });
    }


    connect(peripheralId) {
        return new Promise((fulfill, reject) => {
            bleManager.connect(peripheralId, (error, peripheral) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(peripheral);
                }
            });
        });
    }

    disconnect(peripheralId) {
        return new Promise((fulfill, reject) => {
            bleManager.disconnect(peripheralId, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    startNotification(peripheralId, serviceUUID, characteristicUUID) {
        return new Promise((fulfill, reject) => {
            bleManager.startNotification(peripheralId, serviceUUID, characteristicUUID, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    stopNotification(peripheralId, serviceUUID, characteristicUUID) {
        return new Promise((fulfill, reject) => {
            bleManager.stopNotification(peripheralId, serviceUUID, characteristicUUID, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    checkState() {
        bleManager.checkState();
    }

    start(options) {
        return new Promise((fulfill, reject) => {
            if (options == null) {
                options = {};
            }
            bleManager.start(options, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }


    stopScan() {
        return new Promise((fulfill, reject) => {
            bleManager.stopScan((error) => {
                if (error != null) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    enableBluetooth() {
        return new Promise((fulfill, reject) => {
            bleManager.enableBluetooth((error) => {
                if (error != null) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    getConnectedPeripherals(serviceUUIDs) {
        return new Promise((fulfill, reject) => {
            bleManager.getConnectedPeripherals(serviceUUIDs, (error, result) => {
                if (error) {
                    reject(error);
                } else {
                    if (result != null) {
                        fulfill(result);
                    } else {
                        fulfill([]);
                    }
                }
            });
        });
    }

    getDiscoveredPeripherals() {
        return new Promise((fulfill, reject) => {
            bleManager.getDiscoveredPeripherals((error, result) => {
                if (error) {
                    reject(error);
                } else {
                    if (result != null) {
                        fulfill(result);
                    } else {
                        fulfill([]);
                    }
                }
            });
        });
    }

    isPeripheralConnected(peripheralId, serviceUUIDs) {
        return this.getConnectedPeripherals(serviceUUIDs).then((result) => {
            if (result.find((p) => {
                    return p.id === peripheralId;
                })) {
                return true;
            } else {
                return false;
            }
        });
    }

    /************************************************************************************
     ---------------------------下面的方法需要Android和iOS分别去实现---------------------------
     ************************************************************************************/


    setNotification(peripheralId) {
        return new Promise((fulfill, reject) => {
            bleManager.startNotification(peripheralId, CommandServiceUUID, CommandCharacteristicUUID, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    scan(timeout) {
        return new Promise((fulfill, reject) => {
            bleManager.scan([], timeout, true, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }


    scanWithFilter(timeout, filterName) {
        return new Promise((fulfill, reject) => {
            bleManager.scanWithFilter(filterName, timeout, (error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }

    /**
     * 像蓝牙写入数据。根据data里面的loopCount，收到回调数据。没有做超时处理
     * @param peripheralId  蓝牙的从机Id
     * @param data          需要发送的数据。使用proto生成
     * @returns {Promise}
     */
    writeBack(peripheralId, data) {

        return new Promise((fulfill, reject) => {
            bleManager.writeWithResponse(peripheralId, CommandServiceUUID, CommandCharacteristicUUID, data, (error, response) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(response);
                }
            });
        });
    }

    /**
     * 主动读取电量
     * @param peripheralId
     * @returns {Promise}
     */
    readPower(peripheralId) {
        return new Promise((fulfill, reject) => {
            bleManager.read(peripheralId, PowerServiceUUID, PowerCharacteristicUUID, (error, data) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(data);
                }
            });
        });
    }

    /**
     * Testing method
     * @param key
     * @param dict
     */
    sendEvent(key, dict) {
        bleManager.sendEvent(key, dict);
    }

}

module.exports = new BleManager();

